﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Linq;
using System.Windows.Threading;
using System.Threading;

namespace TAXILISM.Class
{
    public class SearchPathFunction
    {
        SearchPlaceFunction fc1 = new SearchPlaceFunction();
        Thread t1;
        string latitude,longitude;


        public int cnt_client;
        public void feedPath(string searchFrom, string searchTo)
        {
            try
            {

                WebClient GoogleAutoComplete1 = new WebClient();
                WebClient GoogleAutoComplete2 = new WebClient();

                GoogleAutoComplete1.DownloadStringAsync(new Uri("http://maps.googleapis.com/maps/api/directions/xml?origin=" + searchFrom + "&destination=" + searchTo + "&sensor=true&units=metric&alternatives=true&avoid=tolls&language=th&region=th"));
                GoogleAutoComplete1.DownloadStringCompleted += new DownloadStringCompletedEventHandler(GooglePathAutoComplete_DownloadStringCompleted);
                cnt_client++;
                //GoogleAutoComplete2.DownloadStringAsync(new Uri("https://maps.googleapis.com/maps/api/place/autocomplete/xml?input=" + searchTo + "&sensor=true&key=AIzaSyD8MrUZToMrnI4RZggiWubLuC_pEbVktt8"));
                //GoogleAutoComplete2.DownloadStringCompleted += new DownloadStringCompletedEventHandler(GoogleToAutoComplete_DownloadStringCompleted);
                //cnt_client++;
            }
            catch
            {

            }
        }


        public List<googlePath> list_path = new List<googlePath>();
        public List<string> picPath = new List<string>();

        void GooglePathAutoComplete_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            try
            {
                string a = e.Result;
                XElement elm = XElement.Parse(e.Result);

                XElement[] dataRoute = elm.Descendants("route").ToArray();

                list_path.Clear();
                for (int countRoute = 0; countRoute < dataRoute.Count(); countRoute++)
                {
                    //string pathPlace = null;

                    var pathAuto = from pathAutodb1 in dataRoute[countRoute].Elements("leg")
                                   from pathAutodb2 in dataRoute[countRoute].Elements("overview_polyline")
                                   from pathAutodb3 in dataRoute[countRoute].Elements("summary")
                                   select new googlePath
                                   {
                                       Data_Name = pathAutodb3.Value,
                                       Data_allDuration = pathAutodb1.Element("duration").Element("text").Value,
                                       Data_allDistance = pathAutodb1.Element("distance").Element("text").Value,
                                       Data_polyline = pathAutodb2.Element("points").Value
                                   };

                    foreach (googlePath rs in pathAuto)
                    {
                        double price;
                        double conDistance; 
                        if(rs.Data_allDistance.Contains("กม."))
                            conDistance = Convert.ToDouble(rs.Data_allDistance.Substring(0, rs.Data_allDistance.Length - 3));
                        else
                            conDistance = Convert.ToDouble(rs.Data_allDistance.Substring(0, rs.Data_allDistance.Length - 2))/100;
                        if (conDistance <= 1) price = 35;
                        else if (conDistance <= 12) price = 35 + (conDistance - 1) * 5;
                        else if (conDistance <= 20) price = 90 + (conDistance - 12) * 5.5;
                        else if (conDistance <= 40) price = 134 + (conDistance - 20) * 6;
                        else if (conDistance <= 60) price = 254 + (conDistance - 40) * 6.5;
                        else if (conDistance <= 60) price = 384 + (conDistance - 60) * 7.5;
                        else price = 534 + (conDistance - 80) * 8.5;
                        price = ((price % 1) >= 0.5) ? Math.Ceiling(price) : Math.Floor(price);
                        rs.Data_calFare = price.ToString() + " บาท";
                        list_path.Add(rs);
                    }

                    //picPath.Add("http://maps.googleapis.com/maps/api/staticmap?size=400x400&sensor=false&path=color:0xff0000ff|weight:5|enc:" + pathPlace);

                }
                cnt_client--;
            }
            catch (WebException)
            {
                MessageBox.Show("Please check your internet connection.");
                return;
            }
            catch (Exception)
            {
                MessageBox.Show("Server connection failed, Please wait a minute and try again.");
                return;
            }
        }


    }
}
